<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Wxpay extends Model
{
    protected $table = "pay";
    protected $guarded = [];

    public static function wxPay(){
        //获取支付信息
        $payData = Wechart::info();
        //定义支付配置数组
        $payConfig = [
            'miniapp_id' => $payData['app_id'], // 小程序 APPID
            'mch_id' => $payData['mch_id'],    //商户号
            'key' => $payData['pay_secret'],   //支付密匙
            'notify_url' => 'https://pc.shafaman.com/api/notify',//当前域名的回调路由
            'cert_client' => resource_path('assets/cert/apiclient_cert.pem'), // optional，退款等情况时用到
            'cert_key' => storage_path('assets/cert/apiclient_key.pem'),// optional，退款等情况时用到
            'log' => [ // optional
                'file' => storage_path('logs/wechat.log'),
                //'level' => 'debug'
            ],
            //'mode' => 'dev', // optional, dev/hk;当为 `hk` 时，为香港 gateway。
        ];
        return $payConfig;
    }

    public static function refundPay(){
        //获取支付信息
        $payData = Wechart::info();
        //定义支付配置数组
        $payConfig = [
            'miniapp_id' => $payData['app_id'], // 小程序 APPID
            'mch_id' => $payData['mch_id'],    //商户号
            'key' => $payData['pay_secret'],   //支付密匙
            'notify_url' => 'https://api.lyworker.com/api/refund',//当前域名的回调路由
            'cert_client' => resource_path('assets/cert/apiclient_cert.pem'), // optional，退款等情况时用到
            'cert_key' => storage_path('assets/cert/apiclient_key.pem'),// optional，退款等情况时用到
            'log' => [ // optional
                'file' => storage_path('logs/wechat.log'),
                //'level' => 'debug'
            ],
            //'mode' => 'dev', // optional, dev/hk;当为 `hk` 时，为香港 gateway。
        ];
        return $payConfig;
    }

    public static function payMent($userId,$payType,$paySn,$amount,$location){
        $pay = new Wxpay();
        $pay->userid = $userId;
        $pay->location = $location;
        $pay->pay_type = $payType;
        $pay->pay_sn = $paySn;
        $pay->amount = $amount;
        $pay->is_paid = 0;
        $pay->created_at = date('Y-m-d H:i:s');
        $pay->save();
        if ($payType=='integral') {
            $pay->is_paid = 1;
            $pay->paid_at = date('Y-m-d H:i:s');
        }
    }
}
